<?php
/**
 * @Desc : API接口模型类
 * @Auth : BraveDu<brave@cmdshell.com>
 * @Date : 2017/3/17 20:27
 */
namespace app\api\model;
use Think\Model;
class Api extends Model
{
    protected $autoCheckFields = false;
    const ERRCODE_SUCCESS   = 0;    //全局成功
    const ERRCODE_LOGINSUSS = 2;    //登录成功
    const ERRCODE_SIGN      = 100;  //一百以上都为错误代码,签名验证错误
    const ERRCODE_ALL       = 101;
    const ERRCODE_NOTMOBILE = 102;
    const ERRCODE_SIGNEXP   = 103;  //签名过期
    const ERRCODE_NOENOUGHPARAMS = 104;
    //登录注册模块
    const ERRCODE_LOGINFAIL = 110;  //登录失败
    const ERRCODE_NOREGEST  = 111;
    const ERRCODE_MSGERROR  = 112;

    const ERRCODE_WATEIMG   = 120;



    static public $errcode_info = array(
        self::ERRCODE_SUCCESS   => '成功',
        self::ERRCODE_SIGN     => '非常抱歉，请您正确访问~',
        self::ERRCODE_ALL      => '失败~',
        self::ERRCODE_NOENOUGHPARAMS => '缺少参数',
        self::ERRCODE_NOTMOBILE => '请你从手机客户端访问',
        self::ERRCODE_LOGINSUSS => '登录成功',
        self::ERRCODE_LOGINFAIL => '登录失败',
        self::ERRCODE_NOREGEST  => '未注册或者登录失败',
        self::ERRCODE_MSGERROR  => '验证码发送失败',
        self::ERRCODE_WATEIMG   => '模板图片出现问题,请及时联系班长',
    );

    function isMobile(){
        if(!checkMoblieAgent()){
            $this->result['errno']  = self::ERRCODE_NOTMOBILE;
            $this->result['errmsg'] = self::$errcode_info[self::ERRCODE_NOTMOBILE];
            return $this->result;
        }
        return true;
    }

    static function checkSignFun($signStr,$unionid,$timestmp){
        $reSignStr = sha1(C('SIGN_PASS') . $unionid . $timestmp );

        $logParams = func_get_args();
        $logParams['reSignStr'] = $reSignStr;
        self::saveLog($logParams);

        if($signStr==$reSignStr){
            //判断是否过期
            if(time()-$timestmp > C('SIGN_EXPSEC')){
                return self::getResultData(self::ERRCODE_SIGNEXP);
            }
        }else{
            return self::getResultData(self::ERRCODE_SIGN);
        }
        return true;
    }

    static public function getResultData($errCode=0,$data='',$msg=''){
        $errCode = $errCode ? $errCode : 0;
        $result = array(
            'status' => $errCode,
            'info'   => $msg ? $msg : self::$errcode_info[$errCode],
            'data'   => $data,
        );
        return $result;
    }

    //参数不全提示信息
    static public function noEnoughParams(){
        return self::getResultData(self::ERRCODE_NOENOUGHPARAMS);
    }


    static public function saveLog($data=array(),$funName=''){
        $funName ? $funName :  ACTION_NAME;
        \Think\Log::write("=================[ LoginBegin : {$funName} ]=====================");
        if(is_array($data) || is_object($data)){
            \Think\Log::write(var_export($data,true),\Think\Log::INFO);
        }else{
            \Think\Log::write($data,\Think\Log::INFO);
        }
        \Think\Log::write("=================[ LoginEnd : {$funName} ]=====================");
    }

}